package br.com.gerenciador_recursos.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.gerenciador_recursos.model.User;
import br.com.gerenciador_recursos.service.UserLocalServiceUtil;
import br.com.gerenciador_recursos.util.Constants;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public LoginServlet() {
    }


	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		
		System.out.println("MyViewServlet");
	
		String email = req.getParameter("emailAddress");
		String password = req.getParameter("password");
		
		User user = UserLocalServiceUtil.findByEmailAddressPassword(email, password);
		
		if(user != null){
			
			req.getSession().setAttribute("userSigned", user);
			/*RequestDispatcher dispatcher = req.getRequestDispatcher("/my-view");
			dispatcher.forward(req, res);*/
			res.sendRedirect(req.getContextPath() + "/my-view");

		}else{
			
			RequestDispatcher dispatcher = req.getRequestDispatcher(Constants.LOGIN_PATH);
			dispatcher.forward(req, res);
			
		}
		
	}
}
